<template>
  <div class="setting-flow-header">
    <div class="left-wrap">
      <div class="back-btn" @click="$emit('checkModified', goBack, true)"><i class="iconfont icon-arrow-left"></i>{{$t('common.base.back')}}</div>
      <div class="base-info">
        <div
          :class="{
            'no-hover': noHover,
          }"
        >
          <!-- 修改颜色 -->
          <el-popover placement="bottom" width="224" trigger="click">
            <div class="choose-color-box">
              <div
                v-for="color in typeColors"
                :key="color"
                @click="serviceEventTypeConfig.config.color = color"
                :style="{ background: color }"
              >
                <i
                  class="el-icon-check"
                  v-if="serviceEventTypeConfig.config.color === color"
                ></i>
              </div>
            </div>
            <el-tooltip
              slot="reference"
              effect="dark"
              :content="$t('event.setting.serviceEventTypeSetting.flow.component.flowHeader.tips1')"
              placement="bottom"
            >
              <i
                class="type-color"
                :style="{ background: serviceEventTypeConfig.config.color }"
              ></i>
            </el-tooltip>
          </el-popover>

          <!-- 事件类型名称  -->
          <div>
            <div class="system-name" v-if="serviceEventTypeConfig.isSystem == 1">{{serviceEventTypeConfig.name}}</div>
            <el-tooltip
              effect="dark"
              :content="$t('event.setting.serviceEventTypeSetting.flow.component.flowHeader.tips2')"
              placement="bottom"
              v-else
            >
              <el-input
                ref="editor"
                class="type-name"
                :value="serviceEventTypeConfig.name"
                @focus="isFocus=true"
                @blur="isFocus=false"
                @input="handelInput($event)"
                :placeholder="$t('event.setting.serviceEventTypeSetting.flow.component.flowHeader.pla1')"
              >
                <template slot="append" v-if="!isFocus"><i class="iconfont icon-edit-square" @click="onModify"></i></template>
              </el-input>
            </el-tooltip>
          </div>
        </div>
      </div>
    </div>
    <div class="middle-wrap">
      <div
        class="setting-setp-item"
        :class="idx === currTab && 'active'"
        v-for="(step, idx) in settingStep"
        :key="step.stepName"
        @click="selectSettingStep(idx)"
      >
        <i>{{ idx + 1 }}</i>
        <div class="step-name">{{ step.stepName }}</div>
      </div>
    </div>
    <div class="right-wrap">
      <el-button
        class="header-save-btn"
        plain
        @click="$emit('submit', true)"
        :loading="pending"
      >{{$t('common.base.save')}}</el-button
      >
    </div>
    <ModifyMultipleLanguagesDialog :service-event-type-config="serviceEventTypeConfig" v-if="visible" :visible.sync="visible" />
  </div>
</template>

<script>
/** util */
import { TYPE_COLORS } from '../../../constant';
/** vue */
import ModifyMultipleLanguagesDialog from '@src/modules/setting/serviceEvent/serviceEventTypeSetting/flow/components/ModifyMultipleLanguagesDialog.vue'
import { openAccurateTab } from '@src/util/platform';
import { PageRoutesTypeEnum } from 'pub-bbx-global/pageType/dist/enum/PageRoutesEnum'
import { useFormMultiLanguage } from '@hooks/useFormMultiLanguage'

const { isOpenMultiLanguage } = useFormMultiLanguage()

export default {
  name:'flow-header',
  components:{
    ModifyMultipleLanguagesDialog
  },
  props: {
    serviceEventTypeConfig: {
      type: Object,
      default: () => ({}),
    },
    settingStep: {
      type: Array,
      default: () => [],
    },
    currTab: {
      type: Number,
      default: 0,
    },
    pending: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      isFocus: false,
      noHover: false,
      visible:false
    };
  },
  computed: {
    typeColors() {
      return TYPE_COLORS;
    },
  },
  methods: {
    onModify() {
      if(isOpenMultiLanguage){
        this.visible = true
        return
      }
      this.$nextTick(() => {
        this.$refs.editor.$el.querySelector('input').focus();
      });
    },
    /** 修改事件类型选择设置步骤*/
    selectSettingStep(idx) {
      if (this.currTab === idx) return;
      this.$emit('checkModified', () => {
        //   变更选择 tab
        this.$emit('update:currTab', idx);
      });
    },
    /** 返回*/
    goBack() {
      let fromId = window.frameElement.getAttribute('fromid');
      let id = window.frameElement.dataset.id;
      this.$platform.closeTab(id);
      // this.$platform.openTab({
      //   id: 'M_SYSTEM',
      //   title: '事件类型设置',
      //   url: '/setting/serviceStation/eventType',
      //   reload: true,
      //   close: true,
      //   fromId,
      // });
      openAccurateTab({
        type: PageRoutesTypeEnum.PageServiceStation,
        titleKey: this.$t('event.setting.serviceEventTypeSetting.flow.component.flowHeader.title1'),
        reload: false,
      })
    },
    handelInput(val){
      this.serviceEventTypeConfig.name = val
      if(isOpenMultiLanguage){
        this.serviceEventTypeConfig.nameLanguage['zh'] = val
      }
    }
  },
};
</script>

<style lang="scss" scoped>
.setting-flow-header {
  position: sticky;
  top: 0;
  display: flex;
  justify-content: space-between;
  width: 100%;
  height: 48px;
  line-height: 48px;
  color: #ffffff;
  background: linear-gradient(270deg, $color-primary-light-4 0%, $color-primary-light-6 100%);
  z-index: 999;
  .left-wrap {
    display: flex;
    & > .back-btn {
      cursor: pointer;
      width: 110px;
      height: 48px;
      padding-left: 20px;
      padding-right: 12px;
      border-right: 1px solid #f5f5f5;
    }
    & > .base-info {
      position: relative;
      display: flex;
      align-items: center;
      width: 100%;
      height: 100%;
      & > div {
        margin-left: 4px;
        padding: 5px 8px;
        width: 190px;
        height: 32px;
        border-radius: 4px;
        display: flex;
        align-items: center;
        span {
          margin-top: 4px
        }
        .type-color {
          cursor: pointer;
          display: inline-block;
          width: 14px;
          height: 14px;
          margin-top: 4px;
          margin-right: 8px;
          border-radius: 50%;
        }
        &::after {
          content: '';
          position: absolute;
          bottom: 8px;
          left: 8px;
          width: calc(100% - 24px);
          transform: scaleY(0.5);
        }
        &:hover {
          cursor: pointer;
          background: rgba(0, 0, 0, 0.08);
          &::after {
            display: none;
          }
        }
        &.no-hover {
          background: transparent;
          &::after {
            content: '';
            position: absolute;
            bottom: 8px;
            left: 8px;
            width: calc(100% - 24px);
            border-bottom: 1px dashed #ffffff;
            transform: scaleY(0.5);
          }
        }
      }
    }
  }

  .middle-wrap {
    display: flex;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    & > div.setting-setp-item {
      cursor: pointer;
      position: relative;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 168px;
      height: 48px;
      color: #ffffff;
      i {
        font-style: normal;
        width: 20px;
        height: 20px;
        line-height: 18px;
        text-align: center;
        margin-right: 8px;
        background: none;
        color: #ffffff;
        border: 1px solid #ffffff;
        border-radius: 50%;
      }
      &.active {
        background: rgba(0, 0, 0, 0.1);
        i {
          color: $color-primary;
          background: #ffffff;
        }
        &::after {
          content: '';
          position: absolute;
          bottom: 0;
          left: 50%;
          transform: translateX(-50%);
          display: block;
          width: 0;
          height: 0;
          border-left: 5px solid transparent;
          border-right: 5px solid transparent;
          border-bottom: 8px solid #f5f5f5;
        }
      }
    }
  }
  .right-wrap {
    width: 80px;
    height: 36px;
    padding-right: 24px;
    .header-save-btn {
      height: 32px;
      color: $color-primary;
      background: #ffffff;
      border: none;
      border-radius: $button-radius-base;
    }
  }
}

.choose-color-box {
  div {
    cursor: pointer;
    position: relative;
    display: inline-block;
    width: 30px;
    height: 30px;
    border-radius: 50%;
    margin-right: 12px;
    i {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      font-size: 24px;
      color: #ffffff;
    }
    &:nth-child(5n) {
      margin-right: 0;
    }
  }
}

/** element-ui style **/
::v-deep .type-name,
.el-input {
  display: flex;
  input.el-input__inner {
    padding: 0;
    height: 24px;
    font-size: $font-size-large;
    color: #fff !important;

    background: transparent;
    border: none;
    @include text-ellipsis();

    &::-webkit-input-placeholder {
      color: rgba(255, 255, 255, 0.8);
    }
    &::-ms-input-placeholder {
      color: rgba(255, 255, 255, 0.8);
    }
  }
  .el-input-group__append {
    background: none;
    color: #fff;
    border: none;
    margin-top: 4px;
    padding-left: 10px;
  }
}

::v-deep .el-switch {
  vertical-align: middle;
}

.type-name {
  ::v-deep .el-input__inner {
    padding-right: 0;
  }
}
.system-name {
  font-size: 16px;
}
</style>
